<?php 
	namespace Admin\Controller;
	use Think\Controller;
	class GoodsController extends Controller{
		//商品添加
		public function tianjia(){
			$goods = D('goods');
			//两份逻辑 1采集表单信息 2展示表单
		if(!empty($_POST)){
			//收集表单信息	
			$data=[
				'goods_name'=>$_POST['goods_name'],
				'goods_price'=>$_POST['goods_price'],
				];
			$a = $goods->add($data);
			if($a){
				//页面跳转
				$this->redirect('showlist',array(),2,'添加成功');
		
			}else{
				$this->redirect('tianjia',array(),2,'添加失败');
			}
		}else{
			//展示表单
			$this->display();
		}
			
			
		}
		//列表展示
		public function showlist(){
			// $obj = new \Admin\Model\GoodsModel();
			// havaing 查询结果集中有的字段
			// where 查询表里有的字段
			$obj = D('goods');
			
			$total = $obj->count();//获取当前记录总条数
			$per=5;
			$page = new \Component\Page($total,$per);
			$sql="select * from sw_goods ".$page->limit;
			$info=$obj->query($sql);
			
			// $info =	$obj->field('goods_id,goods_name,goods_price')->group('goods_price')->where('goods_price')->select();
			$pagelist = $page->fpage();
			$this->assign('info', $info);
			$this->assign('pagelist', $pagelist);
			$this->display();
		}
		//修改商品
		public function upd(){
			//兩個邏輯 判斷是否POST
			if(!empty($_POST)){
				$goods = D('goods');
				$data=[
				'goods_id'=>$_POST['goods_id'],
				'goods_name'=>$_POST['goods_name'],
				'goods_price'=>$_POST['goods_price']
				];
				$a = $goods->save($data);
				if($a){
					$this->redirect('showlist',array(),3,'修改成功');
				}else{
					$this->redirect('upd',array(),3,'修改shibai');
				}
			}else{
				$goods = D('goods');
				$id=$_GET['goods_id'];
				$a = $goods->find($id);
				$this->assign('info',$a);
				$this->display();
			 }
		}
		public function del(){
			$goods=M('goods');
			// $sql="select * from sw_goods";
			dump(I('goods_id'));
			// $id=$_GET['goods_id'];
			
			// $goods->where("goods_id = $id")->delete();
			// dump($id);
		}
		//设置缓存信息
		public function s(){

			S('name','lisi1',15);
			echo 'success';
		}
		public function s1(){
			echo S('name');
			echo 'du';
		}
		public function showlist1(){
			$goods=D('role');
			$a = $goods->select();
			$this->assign('info',$a);
			$this->display();
		}

	}



 ?>